<?php

class SimpletextEvaluator {

    public static function evaluate($submission){
        $problems = $submission->problem->getConfig('problems');
        $answers = $submission->getSubmitContent('answers');
        $score = 0;
	$totalscore = 0;
        $count = count($problems);
        $grade_output = "";
        foreach($problems as $k => $p){
            echo sprintf(
                "\t\t#%03d\n".
                "\t\t !! : %s\n".
                "\t\t ?? : %s\n",
                $k, str_replace('\n', '\n\t\t ', $p['answer']), $answers[$k]
            );
            if (strcmp($p['answer'], $answers[$k]) == 0){
				if (isset($p['point'])){
					$score += $p['point'];
				} else {
					$score += 1;
				}
                $grade_output .= sprintf("#%03d : %s", $k, "Correct\n");
            } else {
				$foundalternative = false;
				if (isset($p['alternatives'])){
					$alternatives = $p['alternatives'];
					$alt = 0;
					foreach($alternatives as $alternative){
						$alt++;
						if (isset($alternative['answer']) && strcmp($alternative['answer'], $answers[$k]) == 0){
							$foundalternative = true;
							if (isset($alternative['point'])){
								$score += $alternative['point'];
							} else {
								$score += 1;
							}
							 $grade_output .= sprintf("#%03d : Correct from alternative %d\n", $k, $alt);
							break;
						}
					}
				}
				if (!$foundalternative){
					$grade_output .= sprintf("#%03d : %s", $k, "Incorrect\n");
				}
            }
			if (isset($p['point'])){
				$totalscore += $p['point'];
			} else {
				$$totalscore += 1;
			}
        }
        if ($count != 0){
            $nscore = ($score / $count) * 100;
        } else {
            $nscore = 0;
        }
        $submission->verdict = $score."/".$count;
        $submission->score = $nscore;
        $submission->setGradeContent("evaluator", $grade_output);
    }

    public static function evaluatex($submission){
        $problems = $submission->problem->getConfig('problems');
        $answers = $submission->getSubmitContent('answers');
        $score = 0;
	$totalscore = 0;
        $count = count($problems);
        $grade_output = "";
        foreach($problems as $k => $p){
            echo sprintf(
                "\t\t#%03d\n".
                "\t\t !! : %s\n".
                "\t\t ?? : %s\n",
                $k, str_replace('\n', '\n\t\t ', $p['answer']), $answers[$k]
            );
            if (strcmp($p['answer'], $answers[$k]) == 0){
		if (isset($p['point'])){
			$score += $p['point'];
		} else {

		}
                $score += 1;
                $grade_output .= sprintf("#%03d : %s", $k, "Correct\n");
            } else {
                $grade_output .= sprintf("#%03d : %s", $k, "Incorrect\n");
            }
        }
        if ($count != 0){
            $nscore = ($score / $count) * 100;
        } else {
            $nscore = 0;
        }
        $submission->verdict = $score."/".$count;
        $submission->score = $nscore;
        $submission->setGradeContent("evaluator", $grade_output);
    }

}
